package com.sws.worktive;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;

import com.sws.worktive.job_advance_search.JobSearchRequestBody;
import com.sws.worktive.util.Constant;

public class HomeFragment extends BaseFragment implements OnClickListener {

	ImageView btn_view_post;
	boolean selection=false;
	Fragment currentFragmnet;
	
	int val_miles, val_JobCategory, val_JobType, val_Language;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setViewPostFragment(selection);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.home_screen, container, false);

		view.findViewById(R.id.icon_profile_header).setOnClickListener(this);
		btn_view_post = (ImageView) view.findViewById(R.id.btn_view_post);
		btn_view_post.setOnClickListener(this);
		btn_view_post.setSelected(selection);
		
		view.findViewById(R.id.icon_search_header).setOnClickListener(this);
		
		registerAdvanceSearchListeners();
		
		return view;
	}
	
	private void registerAdvanceSearchListeners() {
		final DrawerFragment drawerFragment = (DrawerFragment) getActivity();
		drawerFragment.spn_MilesRadius.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 != 0) val_miles = arg2;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
		
		drawerFragment.spn_JobTitle.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 != 0) val_JobCategory = arg2;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		
		drawerFragment.spn_JobType.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 != 0) val_JobType = arg2;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
		
		drawerFragment.spn_Language.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 != 0) val_Language = arg2;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		
		drawerFragment.findViewById(R.id.btn_search).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//TODO: this code is ugly, but lazy to fix...
				getActivity().getSupportFragmentManager().popBackStackImmediate();
				drawerFragment.drawerLayout.closeDrawer((drawerFragment).mRightDrawerRoot);
				
				FragmentManager fragmentManager = getChildFragmentManager();
				HomeFragment.this.selection = false;
				btn_view_post.setSelected(selection);
				JobSearchRequestBody requestBody = new JobSearchRequestBody(drawerFragment.edt_zipcode.getText().toString(), val_miles + "", "7dea6e8128c3c4120aa1016ded6aee9e", val_Language + "", val_JobType + "");
//				JobSearchRequestBody requestBody = new JobSearchRequestBody("30046", "1", "7dea6e8128c3c4120aa1016ded6aee9e", "1", "1"); 
				
				currentFragmnet = ViewFragment.newInstance(fragmentManager, Constant.VIEW_FRAGMENT, requestBody);
				if (currentFragmnet != null) {
					FragmentTransaction transaction = fragmentManager.beginTransaction();
					transaction.replace(R.id.home_container, currentFragmnet, Constant.VIEW_FRAGMENT);
					transaction.commit();
				}else {
					((ViewFragment)fragmentManager.findFragmentByTag(Constant.VIEW_FRAGMENT)).fetchSearchJobList(requestBody);
				}
			}
		});
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.icon_profile_header:
			((DrawerFragment) getActivity()).drawerLayout
					.openDrawer(((DrawerFragment) getActivity()).mLeftDrawerRoot);
			break;

		case R.id.icon_search_header:
			((DrawerFragment) getActivity()).drawerLayout
					.openDrawer(((DrawerFragment) getActivity()).mRightDrawerRoot);
			break;

		case R.id.btn_view_post:
			setViewTabs(!selection);
			break;
		}
	}
	
	private void setViewTabs(boolean selection){
		this.selection = selection;
		btn_view_post.setSelected(selection);
		setViewPostFragment(selection);
	}

	public void setViewPostFragment(boolean selection) {
		FragmentManager fragmentManager = getChildFragmentManager();
		if (selection) {
			currentFragmnet = fragmentManager.findFragmentByTag(Constant.POST_FRAGMENT);

			if (currentFragmnet == null) currentFragmnet = new PostFragment();
			
			FragmentTransaction transaction = fragmentManager.beginTransaction();
			transaction.replace(R.id.home_container, currentFragmnet, Constant.POST_FRAGMENT);
			transaction.commit();

		} else {
			currentFragmnet = ViewFragment.newInstance(fragmentManager, Constant.VIEW_FRAGMENT);
			if (currentFragmnet != null) {
				FragmentTransaction transaction = fragmentManager.beginTransaction();
				transaction.replace(R.id.home_container, currentFragmnet, Constant.VIEW_FRAGMENT);
				transaction.commit();
			}
		}
	}
}
